package org.practice.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.practice.action.vo.BookListVO;
import org.practice.dao.BookDao;
import org.practice.dao.CategoryDao;
import org.practice.model.Book;
import org.practice.model.Category;
import org.practice.util.ShopDaoFactory;
@SuppressWarnings("serial")
public class BookListAction extends BaseAction{
	private BookListVO bookListVO=new BookListVO();
	public static final int ROWS_PER_PAGE=5;
	@Override
	public String execute() throws Exception{
		Integer categoryId=bookListVO.getThreeCatId();
		Integer page=bookListVO.getPage();
		Integer orderBy=bookListVO.getOrderBy();
		BookDao dao=ShopDaoFactory.createBookDao();
		List<Book> allBooks=dao.findBookByCategoryId(categoryId, page, ROWS_PER_PAGE, orderBy);
		
		Integer totalPages=dao.getTotalPagesByCategoryId(categoryId, ROWS_PER_PAGE);
		CategoryDao catDao=ShopDaoFactory.createCategoryDao();
		Category threeCate=catDao.findCategoryById(bookListVO.getThreeCatId());
		
		bookListVO.setBookList(allBooks);
		bookListVO.setCategory(threeCate.getParentCategory());
		
		HttpServletRequest request=ServletActionContext.getRequest();
		request.setAttribute("totalPages", totalPages);
		
		Map<Integer,String> orderOptions=dao.getOrderOptions();
		request.setAttribute("orderOptions", orderOptions);
		
		//struts2资源化 (jsp:<s:text name="%{#options.value}"/> ,action:getText)
		this.addActionMessage(getText("book.order.default"));


		return SUCCESS;
	}
	public BookListVO getBookListVO() {
		return bookListVO;
	}
	public void setBookListVO(BookListVO bookListVO) {
		this.bookListVO = bookListVO;
	}

}
